/**
 * 
 */
package command.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import command.Command;

/**
 * @author faubry
 * 
 */
public class Login implements Command {

	/*
	 * (non-Javadoc)
	 * 
	 * @see command.Command#execute(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("command:" + this.getClass());

		String login = "", password = "";
		login = (String) request.getParameter("login");
		password = (String) request.getParameter("password");
		System.out.println("login:"+login+" password:"+password);
		if ("fred".equalsIgnoreCase(login) && "pwd".equalsIgnoreCase(password)) {
			// ouverture de session et mise en attribut du login (retiré à l'expiration) pour contrôle de connexion
			request.getSession().setAttribute("login", login);
			// vers la page d'index
			return ("magasin?cmd=id");
		} else {
			// sinon erreur et retour à la page de login
			request.setAttribute("erreur", "<strong>Login incorrect !</strong>");
			return ("magasin?cmd=doLogin");
		}
	}

}
